package com.ruoyi.generator.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.generator.entity.*;
import com.ruoyi.generator.entity.dto.CustomerExportVo;
import com.ruoyi.generator.entity.dto.GetDetailvo;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("customer")
public class CustomerController extends BaseController {

    @Autowired
    private com.ruoyi.generator.service.CustomerService CustomerService;

    @PostMapping("/add")
    public AjaxResult createStipulation(@RequestBody Customer  Customer) {

        return toAjax( CustomerService.insertCustomer(Customer));
    }
    @PostMapping("/addInvoiceForm")
    public int addInvoice(@RequestBody InvoiceForm invoiceForm) {
        int result =CustomerService.addInvoice(invoiceForm);
        return result;
    }

    @PostMapping("/addVisit")
    public AjaxResult addVisit(@RequestBody CustomerVisit customerVisit) {
        return toAjax( CustomerService.addVisit(customerVisit));
    }

    @PostMapping("/update")
    public AjaxResult updateStipulation(@RequestBody Customer Customer) {
        return toAjax( CustomerService.updateCustomerByCode(Customer));
    }
    @PostMapping("/updateVisit")
    public AjaxResult updateVisit(@RequestBody CustomerVisit  CustomerVisit) {
        return toAjax( CustomerService.updateVisit(CustomerVisit));
    }

    @GetMapping("/list")
    public TableDataInfo list(CustomerSearchCriteria CustomerSearchCriteria)
    {
        startPage();
        List<Customer> list = CustomerService.selectCustomerList(CustomerSearchCriteria);
        return getDataTable(list);
    }
    /**
     * 导出客户信息
     */
    @Log(title = "客户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerExportSearchCriteria  CustomerExportSearchCriteria)
    {

        List<CustomerExportVo> CustomerListVO = CustomerService.pushCustomerList(CustomerExportSearchCriteria);

        ExcelUtil<CustomerExportVo> util = new ExcelUtil<CustomerExportVo>(CustomerExportVo.class);
        util.exportExcel(response, CustomerListVO, "客户信息");
    }

    @PostMapping("/getDetail")
    public AjaxResult getDetail(@RequestParam String customerCode) {
        Customer customer = CustomerService.selectCustomerBycustomerCode(customerCode);
        CustomerInvoice customerInvoice = CustomerService.queryCustomerInvoiceByCustomerCode(customer.getCustomerCode());
        GetDetailvo getDetailvo = new GetDetailvo();
        getDetailvo.setCustomer(customer);
        getDetailvo.setCustomerInvoice(customerInvoice);
        return AjaxResult.success(getDetailvo);
    }

    @GetMapping("/visitList")
    public TableDataInfo visitList(CustomerVisitSearchCriteria CustomerVisitSearchCriteria)
    {
        startPage();
            List<CustomerVisit> list = CustomerService.selectCustomerVisitList(CustomerVisitSearchCriteria);
        return getDataTable(list);
    }


    @PostMapping("/queryMesBycustomerName")
    public AjaxResult queryMesBycustomerName(@RequestParam String customerName) {
        List<Customer> customer   = CustomerService.queryMesBycustomerName(customerName);
        return AjaxResult.success(customer);
    }

    @PostMapping("/queryStartDateByContractNumber")
    public AjaxResult queryStartDateByContractNumber(@RequestParam String contractCode) {
        String startDate  = CustomerService.queryStartDateByContractNumber(contractCode);
        return AjaxResult.success(startDate);
    }

    @PostMapping("/queryCustomerInvoiceByClientName")
    public AjaxResult queryCustomerInvoiceByClientName(@RequestParam String clientName) {
        CustomerInvoice customerInvoice  = CustomerService.queryCustomerInvoiceByClientName(clientName);
        return AjaxResult.success(customerInvoice);
    }

    @PostMapping("/selectInvoiceByContractNumber")
    public AjaxResult selectInvoiceByContractNumber(@RequestParam String contractCode) {
        List<InvoiceForm> invoiceForm = CustomerService.selectInvoiceByContractNumber(contractCode);
        return AjaxResult.success(invoiceForm);
    }





    /**
     * 删除客户信息
     */
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerCode}")
    public AjaxResult remove(@PathVariable String  customerCode) throws SchedulerException, TaskException
    {
        System.out.println(customerCode);
        CustomerService.deleteCustomerBycustomerCode(customerCode);
        return success();
    }

}
